/*
 * @Author: wupeiwen <javapeiwen2010@gmail.com>
 * @Date: 2022-05-25 16:02:44
 * @LastEditors: wupeiwen <javapeiwen2010@gmail.com>
 * @LastEditTime: 2023-06-29 14:26:27
 * @FilePath: /tea-garden-web/src/util/storeModules/subject-growth.js
 * @Description: 专题-长势与水肥
 */
import { get } from '@/util/http.js'
import { color } from '@/util/dict.js'
export default {
  state: {
    types: [{ name: '整体长势', value: '0' }, { name: '土壤水分', value: '1' }, { name: '土壤养分', value: '2' }],
    activeType: '0',
    loading: {
      growthStatistics: false,
      growthTrend: false,
      growthDistribute: false,
      farmlandGrowth: false,
      farmingRecord: false
    },
    growthStatistics: null,
    growthTrend: [],
    growthDistribute: [],
    farmlandGrowth: [],
    farmingRecord: []
  },
  mutations: {
    setActiveType (state, payload) {
      state.activeType = payload
    },
    // 设置通用状态
    setCommonState (state, payload) {
      const { key, value } = payload
      state[key] = value
    },
    // 设置加载状态
    setLoadingState (state, payload) {
      const { key, value } = payload
      state.loading[key] = value
    }
  },
  actions: {
    // 长势指数统计
    async getGrowthStatistics ({ state, commit, rootState }) {
      try {
        commit('setLoadingState', { key: 'growthStatistics', value: true })
        commit('setCommonState', { key: 'growthStatistics', value: null })
        commit('setCommonState', { key: 'growthTrend', value: [] })
        // 涨势时间
        const data = await get({ url: '/zspj/list', params: { tab_value: state.activeType, massif_id: rootState['base-overview'].currentFarmlandId } })
        const growthTrend = data.reverse().map((item) => {
          // const date = item.predict_time.split(' ')[0].split('-')
          // item.predict_time = date[1] + '.' + date[2]
          return { name: item.datetime, value: item.zspj ? item.zspj : 0 }
        })
        commit('setCommonState', { key: 'growthStatistics', value: data })
        commit('setCommonState', { key: 'growthTrend', value: growthTrend })
      } catch (error) {
        console.log(error)
      }
      commit('setLoadingState', { key: 'growthStatistics', value: false })
    },
    // 趋势变化
    async getGrowthTrend ({ state, commit, rootState }) {
      try {
        commit('setLoadingState', { key: 'growthTrend', value: true })
        commit('setCommonState', { key: 'growthTrend', value: [] })
        // const { data } = await get({ url: '/Growing/DegreeTrend', params: { tab_value: state.activeType, massif_id: rootState['base-overview'].currentFarmlandId } })
        const data = [{
          predict_time: 111,
          degree: 1
        },
        {
          predict_time: 222,
          degree: 2
        }
        ]
        const growthTrend = data.reverse().map((item) => {
          // const date = item.predict_time.split(' ')[0].split('-')
          // item.predict_time = date[1] + '.' + date[2]
          return { name: item.predict_time, value: item.degree }
        })
        // commit('setCommonState', { key: 'growthTrend', value: growthTrend })
      } catch (error) {
        console.log(error)
      }
      commit('setLoadingState', { key: 'growthTrend', value: false })
    },
    // 长势分布
    async getGrowthDistribute ({ state, commit, rootGetters }) {
      try {
        commit('setLoadingState', { key: 'growthDistribute', value: true })
        commit('setCommonState', { key: 'growthDistribute', value: [] })
        // const { data } = await get({ url: '/Growing/DegreeDistribute', params: { tab_value: state.activeType } })
        const area = {}
        const percent = {}
        // data.map((item) => {
        //   area[item.degree] = item.massif_area
        //   percent[item.degree] = item.area_percentage
        //   return item
        // })
        let parentId = 75
        if (state.activeType === '1' || state.activeType === '2') {
          parentId = 80
        }
        let growthDistribute = rootGetters['common/getDictByParentId'](parentId).map((item) => {
          item.label = item.name
          item.color = color[item.id]
          item.area = area[item.id] || 0
          item.percent = percent[item.id] || 0
          return item
        })
        growthDistribute = [{ label: '1111', color: '#000', percent: parentId, area: 1 }, { label: '1111', color: '#000', percent: parentId, area: 1 }]
        commit('setCommonState', { key: 'growthDistribute', value: growthDistribute })
      } catch (error) {
        console.log(error)
      }
      commit('setLoadingState', { key: 'growthDistribute', value: false })
    },
    // 各地块长势等级
    async getFarmlandGrowth ({ state, commit }) {
      try {
        commit('setLoadingState', { key: 'farmlandGrowth', value: true })
        commit('setCommonState', { key: 'farmlandGrowth', value: [] })
        // const { data } = await get({ url: '/Growing/MassifDistribute', params: { tab_value: state.activeType } })
        const data = 1
        commit('setCommonState', { key: 'farmlandGrowth', value: data })
      } catch (error) {
        console.log(error)
      }
      commit('setLoadingState', { key: 'farmlandGrowth', value: false })
    },
    // 农事记录
    async getFarmingRecordById ({ state, commit, rootState }) {
      try {
        commit('setLoadingState', { key: 'farmingRecord', value: true })
        commit('setCommonState', { key: 'farmingRecord', value: [] })
        const { data } = await get({ url: '/Growing/FarmingRecord', params: { massif_id: rootState['base-overview'].currentFarmlandId } })
        commit('setCommonState', { key: 'farmingRecord', value: data })
      } catch (error) {
        console.log(error)
      }
      commit('setLoadingState', { key: 'farmingRecord', value: false })
    }
  }
}
